﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

using PeopleFinderBusiness;

namespace PeopleFinder.Account
{
	public partial class Register : System.Web.UI.Page
	{

		protected void Page_Load(object sender, EventArgs e)
		{
			RegisterUser.RequireEmail = false;
			RegisterUser.ContinueDestinationPageUrl = Request.QueryString["ReturnUrl"];
		}

		protected void RegisterUser_Creating(object sender, LoginCancelEventArgs e)
		{
			UserController controller = new UserController();
			TextBox tbRealName = (TextBox)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("RealName");
			TextBox tbSurname = (TextBox)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("Surname");
			int idUser = controller.RegisterUser(HttpContext.Current.Request.UserHostAddress, RegisterUser.Password, RegisterUser.UserName, tbRealName.Text, tbSurname.Text);
			if(idUser == 0) e.Cancel = true;
		}

		protected void RegisterUser_CreatedUser(object sender, EventArgs e)
		{
			TextBox tbRealName = (TextBox)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("RealName");
			TextBox tbSurname = (TextBox)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("Surname");

			FormsAuthentication.SetAuthCookie(tbRealName.Text + " " + tbSurname.Text, false /* createPersistentCookie */);

			string continueUrl = RegisterUser.ContinueDestinationPageUrl;
			if (String.IsNullOrEmpty(continueUrl))
			{
				continueUrl = "~/";
			}
			Response.Redirect(continueUrl);
		}

	}
}
